﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TouristAgencyDataLayer;

namespace TouristAgencyWinApp
{
    public partial class BoardsForm : Form
    {
        private Board selectedBoard;

        public BoardsForm()
        {
            InitializeComponent();
            WinLoad();
        }

        private void WinLoad()
        {
            boardBindingSource.DataSource = Boards.GetBoards();
        }

        private void boardDG_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != this.boardDG.Rows.Count - 1)
            {
                selectedBoard = Boards.GetBoardById(((Board)this.boardDG.Rows[e.RowIndex].DataBoundItem).BoardId);
                this.boardTxt.Text = selectedBoard.BoardType;
            }
            else
            {
                this.boardTxt.Text = "";
                selectedBoard = null;
            }
        }

        private void saveBtn_Click(object sender, EventArgs e)
        {
            if (selectedBoard != null && !string.IsNullOrEmpty(this.boardTxt.Text))
            {
                Boards.UpdateBoard(selectedBoard.BoardId, this.boardTxt.Text);
                MessageBox.Show("Changes have been saved");
            }
            else if (!string.IsNullOrEmpty(this.boardTxt.Text))
            {
                Boards.CreateBoard(this.boardTxt.Text);
                MessageBox.Show("New Board has been added");
            }
            WinLoad();
        }

        private void delBtn_Click(object sender, EventArgs e)
        {
            if (selectedBoard != null)
            {
                Boards.DeleteBoard(selectedBoard.BoardId);
                MessageBox.Show("The selected item has been deleted");
            }
            else
            {
                MessageBox.Show("Select some item first.");
            }
            WinLoad();
        }
    }
}
